<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
    <head th:replace="/common/include :: header(~{::title}, ~{::link}, ~{::style})">
        <title>系统管理-用户管理-修改密码</title>
    </head>
    <body class="gray-bg">
        <form class="form-horizontal m" id="form-user-edit">
            <div class="animated fadeInRight form-content">
                <input type="hidden" id="userId" name="userId"/>
                <div class="form-group">
                    <label class="col-xs-3 control-label" th:utext="#{view.nick_name}">用户昵称：</label>
                    <div class="col-xs-8">
                        <input class="form-control" type="text" name="userName" readonly="readonly"/>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-3 control-label" th:utext="#{view.login_name}">登录名：</label>
                    <div class="col-xs-8">
                        <input class="form-control" type="text" name="loginName" readonly="readonly"/>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-3 control-label" th:utext="#{view.password.original}">原始密码：</label>
                    <div class="col-xs-8">
                        <input class="form-control" type="password" name="oldPassword"/>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-3 control-label" th:utext="#{view.password.modify}">修改密码：</label>
                    <div class="col-xs-8">
                        <input class="form-control" type="password" name="newPassword"/>
                    </div>
                </div>
                <div class="form-group mb40">
                    <label class="col-xs-3 control-label" th:utext="#{view.repassword}">确认密码：</label>
                    <div class="col-xs-8">
                        <input class="form-control" type="password" name="confirmPassword"/>
                    </div>
                </div>
            </div>

            <div class="form-footer sticky">
                <button type="button" class="btn btn-success form-submit" onclick="save()" th:utext="#{view.save}">保存</button>
                <button type="button" class="btn btn-default" onclick="closeWin()" th:utext="#{view.close}">关闭</button>
            </div>
        </form>

        <div th:replace="/common/include :: footer"></div>

        <script th:inline="javascript">
            var prefix = ctx + 'api/system/user';
            var user_id = [[${id}]];

            $(function () {
                // 表单赋值
                $.operate.get({
                    url: prefix + '/load/basic/' + user_id,
                    success: function(result) {
                        if (result.code === 200) {
                            $.form.load('form-user-edit', result.param);
                            $.form.initValidator('form-user-edit', {
                                newPassword: {
                                    validators: {
                                        notEmpty: {
                                            message: [[#{view.entry-password}]]
                                        },
                                        regexp: {
                                            regexp: '^[a-zA-Z][a-zA-Z0-9!@#$~]{5,17}$',
                                            message: [[#{view.password.rule}]]
                                        }
                                    }
                                },
                                confirmPassword: {
                                    validators: {
                                        notEmpty: {
                                            message: [[#{view.entry-password}]]
                                        },
                                        identical: {
                                            field: 'newPassword',
                                            message: [[#{view.repassword.inconformity}]]
                                        }
                                    }
                                }
                            });
                        } else {
                            $.modal.toast.error(result.msg, result.param);
                            setTimeout(function () {
                                window.location.href = ctx + result.code;
                            }, 1500);
                        }
                    },
                    error: function(result) {
                        window.location.href = ctx + '500?errInfo=' + result;
                    }
                });
            });

            function save() {
                if ($.form.isValid('form-user-edit')) {
                    // 加载参数数据
                    var param = $.form.serialize('form-user-edit');
                    $.operate.post({
                        url: prefix + '/updatePwd/' + user_id,
                        data: param,
                        success: function (result) {
                            if (result.code == 200) {
                                $.modal.toast.success(result.msg);
                                setTimeout(closeWin, 1500);
                            } else {
                                $.modal.toast.error(result.msg, result.param);
                            }
                        }
                    });
                }
            }

            function closeWin() {
                $.modal.lay.close();
            }
        </script>
    </body>
</html>
